<template>
	<el-card class="form-container">
		<el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()" label-width="90px">
			<el-form-item label="活动名称" prop="seckillName">
				<el-input v-model="dataForm.seckillName" placeholder="请输入秒杀活动名称"></el-input>
			</el-form-item>
			<el-form-item label="开始时间" prop="startDate">
				<el-date-picker
					v-model="dataForm.startDate"
					type="date"
					placeholder="选择开始时间"
					format="yyyy 年 MM 月 dd 日"
					:picker-options="pickerTimeOptions">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="结束时间" prop="endDate">
				<el-date-picker
					v-model="dataForm.endDate"
					type="date"
					placeholder="选择结束时间"
					format="yyyy 年 MM 月 dd 日"
					:picker-options="pickerTimeOptions">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="上线/下线" prop="onlineStatus">
				<el-radio-group v-model="dataForm.onlineStatus">
					<el-radio label="1">上线</el-radio>
					<el-radio label="2">下线</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="秒杀时间" prop="onlineStatus">
				<el-checkbox-group v-model="dataForm.sceneCodeList">
					<el-checkbox v-for="sence in senceList" 
						:key="sence.sceneCode"
						:label="sence.sceneTimeSection"
						:value="sence.sceneCode"
					></el-checkbox>
				</el-checkbox-group>
			</el-form-item>
			
			<el-form-item>
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="submit('dataForm')">确定</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
	import { addSeckill ,findSeckillSceneList } from '@/api/activity/seckill'
	import {formatDate} from '@/utils/common';
	export default {
		name: 'add-seckill',
		data() {
			return {
				senceList:[],
				dataForm: {
					seckillName: '',
					startDate: '',
					endDate: '',
					onlineStatus: '',
					sceneCodeList: []
				},
				pickerTimeOptions: {
					disabledDate(time) {
						return time.getTime() < Date.now();
					},
				},
				dataRule: {
					seckillName: [{
							required: true,
							message: '请输入秒杀活动名称',
							trigger: 'blur'
						},
					],
					startDate: [{
							required: true,
							message: '请输入秒杀开始时间',
							trigger: 'blur'
						},
					],
					endDate: [{
							required: true,
							message: '请输入秒杀结束时间',
							trigger: 'blur'
						},
					],
				}
			}
		},
		created() {
			this.initSeckillScene()
		},
		methods: {
			initSeckillScene(){
				findSeckillSceneList().then(res => {
					res.data.responseData.forEach(item => {
						let senceListParam = {
							sceneCode: item.sceneCode,
							sceneName: item.sceneName,
							sceneTimeSection: formatDate(new Date(item.startTime), 'hh:mm:ss') + '-' +formatDate(new Date(item.endTime), 'hh:mm:ss')
						}
						this.senceList.push(senceListParam)
					})
				})
			},
			cancel() {
				this.$router.go(-1)
			},
			submit() {
				const set = this.$refs;
				set['dataForm'].validate((valid) => {
					if (!valid) {
						return false
					}
					let checkSceneCodeList = []
					for(let i = 0;i < this.senceList.length;i++){
						if(this.dataForm.sceneCodeList.indexOf(this.senceList[i].sceneTimeSection)){
							checkSceneCodeList.push(this.senceList[i].sceneCode)
						}
					}
					this.dataForm.sceneCodeList = checkSceneCodeList
					addSeckill(this.dataForm).then(res => {
						if(res.data.responseCode == '200'){
							this.$notify({
								title: '添加成功',
								message: '添加成功',
								type: 'success',
								duration: 2000,
								onClose: this.$router.go(-1)
							})
						}
					})
				})
			},
		}
	}
</script>

<style>
</style>
